package com.example.dao.impl;

import com.example.dao.BorrowDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


@Repository
public class BorrowDaoImpl implements BorrowDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int borrowBook(int book_id, int user_id) {
        String sql = "insert into borrow (book_id, user_id,borrow_date) values (?, ?,NOW());";
        return this.jdbcTemplate.update(sql, book_id, user_id);

    }

    @Override
    public int returnBook(int book_id,int user_id) {
        String sql = "UPDATE borrow SET return_date = NOW() WHERE book_id = ? AND user_id = ?";
        return this.jdbcTemplate.update(sql, book_id, user_id);
    }

//    @Override
//    @Transactional
//    public int getBorrowCount(int book_id, int user_id) {
//        int count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM borrow WHERE book_id = ? AND user_id = ?",
//                Integer.class, book_id, user_id);
//        return count ;
//    }
}
